//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#include "aura.h"
#include "wince.h"
#include "private.h"

extern DWORD g_tickCountCoord;

AURA_API AuraSystem_GetTime(
        /* [out] */ Aura_SystemTime * pTime)
{
    SYSTEMTIME systime;

    assert(NULL != pTime);

    memset(&systime, 0, sizeof(systime));
    GetSystemTime(&systime);
    WinSysTimeConvertToAura(&systime, pTime);

    return AURA_NOERROR;
}

AURA_API AuraSystem_SetTime(
        /* [in] */ Aura_SystemTime * pTime)
{
    SYSTEMTIME systime;
    BOOL bSuccess = FALSE;

    assert(NULL != pTime);

    memset(&systime, 0, sizeof(systime));
    AuraSysTimeConvertToWin(pTime, &systime);
    bSuccess = SetSystemTime(&systime);
    if (!bSuccess) {
        goto E_FAIL_EXIT;
    }
    return AURA_NOERROR;

E_FAIL_EXIT:
    return GetLastAuraECode();
}

AURA_API AuraSystem_QueryPerformanceCounter(
        /* [out] */Aura_Int64 * pCounter)
{
    BOOL bSuccess = FALSE;

    assert (NULL != pCounter);

    bSuccess = QueryPerformanceCounter((LARGE_INTEGER *)pCounter);
    if (!bSuccess) {
        goto E_FAIL_EXIT;
    }
    return AURA_NOERROR;

E_FAIL_EXIT:
    return GetLastAuraECode();
}

AURA_API AuraSystem_QueryPerformanceFrequency(
        /* [out] */Aura_Int64 * pFrequency)
{
    BOOL bSuccess = FALSE;

    assert (NULL != pFrequency);

    bSuccess = QueryPerformanceFrequency((LARGE_INTEGER *)pFrequency);
    if (!bSuccess) {
        goto E_FAIL_EXIT;
    }
    return AURA_NOERROR;

E_FAIL_EXIT:
    return GetLastAuraECode();
}

AURA_API AuraSystem_GetElapsedTime(
        /* [out] */ Aura_Millisecond32 * pTime)
{
    assert(NULL != pTime);

    // todo;
    *pTime = GetTickCount() - g_tickCountCoord;
    return AURA_NOERROR;
}

AURA_API AuraSystem_GetIdleTime(
        /* [out] */ Aura_Millisecond32 * pTime)
{
    assert(NULL != pTime);

    DWORD time = GetIdleTime();
    if (MAXDWORD == time) {
        goto E_FAIL_EXIT;
    }
    *pTime = (Aura_Millisecond32)time;
    return AURA_NOERROR;

E_FAIL_EXIT:
    return AURA_E_NOT_SUPPORTED;
}

AURA_API AuraSystem_GetMemorySizes(
        /* [out] */ Aura_Int32 * pTotalPhysicalMemorySize,
        /* [out] */ Aura_Int32 * pAvailablePhysicalMemorySize)
{
    MEMORYSTATUS memStatus;

    if ((NULL == pTotalPhysicalMemorySize)
        && (NULL == pAvailablePhysicalMemorySize)) {
        return AURA_E_INVALID_ARGUMENT;
    }

    GlobalMemoryStatus(&memStatus);
    if (NULL != pTotalPhysicalMemorySize) {
        *pTotalPhysicalMemorySize = memStatus.dwTotalPhys;
    }
    if (NULL != pAvailablePhysicalMemorySize) {
        *pAvailablePhysicalMemorySize = memStatus.dwAvailPhys;
    }

    return AURA_NOERROR;
}

AURA_API AuraSystem_ErrorPrint(
        /* [in] */ Aura_PWChar wstr)
{
    Aura_printf("%S", wstr);
    return AURA_NOERROR;
}

